package main

import (
	"fmt"
	"github.com/golang/glog"
	"io"
	"net/http"
	"syscall"
)

/**
入口函数
*/
func main() {

	glog.Infoln("Starting http server...")
	http.HandleFunc("/", rootHandler)
	http.HandleFunc("/healthz", healthzHandler)
	err := http.ListenAndServe(":80", nil)
	if err != nil {
		glog.Error(err)
	}

}

/**
健康检查处理器
*/
func healthzHandler(w http.ResponseWriter, r *http.Request) {
	glog.Infoln("entering health handler")
	io.WriteString(w, fmt.Sprintf("%d", http.StatusOK))

}

/**
根路径处理器
*/
func rootHandler(w http.ResponseWriter, r *http.Request) {
	glog.Infoln("entering root handler")
	io.WriteString(w, "===================Details of the http request header:============\n")
	for k, v := range r.Header {
		w.Header().Add(k, fmt.Sprintf("%s", v))
	}
	// 获取自定义环境变量
	env, _ := syscall.Getenv("VERSION")
	//自定义环境变量放入头信息
	w.Header().Add("VERSION", env)
	glog.Infoln(fmt.Sprintf("IP:%s,STATUS:%d\n", r.RemoteAddr, http.StatusOK))
	// 输出头信息到页面
	for k, v := range w.Header() {
		io.WriteString(w, fmt.Sprintf("%s=%s\n", k, v))
	}

}
